package com.github.boluo1230.wechat.util.crypto;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class MD5 {

    public static String encrypt(String source) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source.getBytes(StandardCharsets.UTF_8));
            byte[] bytes = md.digest();
            StringBuilder stringBuffer = new StringBuilder();
            for (byte b : bytes) {
                int bt = b & 255;
                if (bt < 16) {
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
